<template>
  <div>
    <div v-show="!identity" style="" class="main">你没有该权限！！！</div>
    <div class="app-container" v-show="identity">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="家长姓名" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入家长姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="家长昵称" prop="nickName">
          <el-input
            v-model="queryParams.nickName"
            placeholder="请输入家长昵称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="info"
            icon="el-icon-s-comment"
            size="mini"
            @click="handleSendClassHomework"
            :disabled="have == true ? true : false"
            >发送家庭作业</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            icon="el-icon-s-comment"
            size="mini"
            :disabled="have == true ? true : false"
            @click="handleMsg"
            >发送消息</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            v-hasPermi="['tab:class:del']"
            @click="handleDelete"
            >踢出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="classParentList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="序号"
          type="index"
          :index="table_index"
          width="50"
          align="center"
        >
        </el-table-column>
        <el-table-column label="头像" align="center" prop="avatar" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.avatar" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="家长姓名" align="center" prop="userName" />
        <el-table-column label="家长年龄" align="center" prop="age" />
        <el-table-column label="家长昵称" align="center" prop="nickName" />
        <el-table-column label="家长电话" align="center" prop="phonenumber" />
        <el-table-column label="家长邮箱" align="center" prop="email" />
        <el-table-column
          label="加入班级时间"
          align="center"
          prop="createTime"
          width="180"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-comment"
              @click="handleMsg(scope.row)"
              >发送消息</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['tab:class:del']"
              >踢出</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改课程的管理对话框 -->

      <el-dialog
        :title="title"
        :visible.sync="open"
        width="780px"
        append-to-body
        align="center"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="接收人">
                <div style="text-align: center; margin-bottom: 20px">
                  <el-transfer
                    style="text-align: left; display: inline-block"
                    v-model="classUserId"
                    filterable
                    :left-default-checked="leftChecked"
                    :right-default-checked="rightChecked"
                    :render-content="renderFunc"
                    :titles="['未选择', '已选择']"
                    :button-texts="['移除', '添加']"
                    :format="{
                      noChecked: '${total}',
                      hasChecked: '${checked}/${total}',
                    }"
                    @change="handleChange"
                    :data="classUserName"
                  >
                  </el-transfer>
                </div>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="作业内容" prop="msgContent">
                <editor
                  v-model="form.msgContent"
                  :min-height="192"
                  @blur="contentRule"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog
        :title="title"
        :visible.sync="openMsg"
        width="780px"
        append-to-body
        align="center"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-row :span="24">
              <el-form-item label="接收人" v-show="show">
                <div style="text-align: center; margin-bottom: 20px">
                  <el-transfer
                    style="text-align: left; display: inline-block"
                    v-model="classUserId"
                    filterable
                    :left-default-checked="leftChecked"
                    :right-default-checked="rightChecked"
                    :render-content="renderFunc"
                    :titles="['未选择', '已选择']"
                    :button-texts="['移除', '添加']"
                    :format="{
                      noChecked: '${total}',
                      hasChecked: '${checked}/${total}',
                    }"
                    @change="handleChange"
                    :data="classUserName"
                  >
                  </el-transfer>
                </div>
              </el-form-item>
            </el-row>
            <el-col :span="24">
              <el-form-item label="消息标题" prop="msgTitle">
                <el-input
                  v-model="form.msgTitle"
                  placeholder="请输入消息标题"
                />
              </el-form-item>
            </el-col>
            >
            <el-col :span="24">
              <el-form-item label="消息内容" prop="msgContent">
                <editor
                  v-model="form.msgContent"
                  :min-height="192"
                  @blur="contentRule"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFormMsg">确 定</el-button>
          <el-button @click="cancelMsg">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  classParentList,
  deleteUserClass,
  classIdentity,
} from "@/api/tab/class";
import { addMsg } from "@/api/message/message";
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        nickName: undefined,
      },
      // 总条数
      total: 0,
      // 班级家长的管理表格数据
      classParentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openMsg: false,
      // 表单参数
      form: {},
      //家长名称
      classUserName: [],
      //左边默认选中
      leftChecked: [],
      //右边默认选中
      rightChecked: [],
      classUserIdAll: [],
      //数组值v-model
      classUserId: [],
      show: true,
      have: false,
      identity: false,
      renderFunc(h, option) {
        return (
          <span>
            {option.label.split(",")[0]}-{option.label.split(",")[1]}
          </span>
        );
      },
      // 表单校验
      rules: {
        msgTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" },
        ],
        msgContent: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.classIdentity();
    this.getList();
    setTimeout(() => {
      this.get();
    }, 100);
  },
  methods: {
    classIdentity() {
      classIdentity().then((respnse) => {
        if (respnse) {
          this.identity = true;
        } else {
          this.identity = false;
        }
      });
    },
    // 富文本校验
    contentRule() {
      this.$refs.addModelRef.validateField("msgContent");
    },
    //初始化获取班级全部家长数据
    getList() {
      this.loading = true;
      classParentList(this.queryParams).then((response) => {
        if (response == "") {
          this.have = true;
        } else {
          this.have = false;
          this.classParentList = response.rows;
          this.total = response.total;
        }
        this.loading = false;
      });
    },
    //获取全部家长真实姓名和昵称
    get() {
      this.loading = true;
      if (!this.have) {
        classParentList({ pageNum: 1, pageSize: 100 }).then((response) => {
          this.loading = false;
          let { classUserName, classUserIdAll } = this;
          if (response != "") {
            response.rows.forEach((item) => {
              classUserName.push({
                key: item.userId,
                label: item.userName + "," + item.nickName,
                disabled: false,
              });
              classUserIdAll.push(item.userId);
            });
          }
        });
      } else {
        this.loading = false;
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    //序号换页问题
    table_index(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /* 菜单重置 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /* 发送班级作业 */
    handleSendClassHomework(row) {
      const userIds = row.userId || this.ids;

      if (userIds.length == 0) {
        this.classUserId = this.classUserIdAll;
      } else {
        this.classUserId = userIds;
      }
      this.open = true;
      this.title = "班级作业";
    },
    /* 发送班级作业 */
    handleMsg(row) {
      const userIds = row.userId || this.ids;
      this.openMsg = true;
      this.title = "班级消息";
      if (userIds.length == 0) {
        this.show = true;
        this.classUserId = this.classUserIdAll;
      } else {
        if (row.userId != undefined) {
          this.classUserId = [];
          this.show = false;
          this.classUserId.push(userIds);
          this.title = "私人消息";
        } else {
          this.show = true;
          this.classUserId = userIds;
        }
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal
        .confirm("是否确认踢出家长？")
        .then(function () {
          return deleteUserClass(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("踢出成功");
        })
        .catch(() => {});
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    // 确认按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.classUserId.length == 0) {
            this.$modal.msgError("请选择接收人");
          } else {
            this.form.msgStatus = 1;
            this.form.msgTitle = "家庭作业";
            addMsg(this.form, this.classUserId).then((response) => {
              this.$modal.msgSuccess("发送班级作业成功");
              this.open = false;
              this.reset();
            });
          }
        }
      });
    },
    submitFormMsg() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.classUserId.length == 0) {
            this.$modal.msgError("请选择接收人");
          } else {
            this.form.msgStatus = 2;
            addMsg(this.form, this.classUserId).then((response) => {
              this.$modal.msgSuccess("发送班级消息成功");
              this.openMsg = false;
              this.reset();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelMsg() {
      this.openMsg = false;
      this.reset();
    },
    handleChange(value) {
      this.classUserId = value;
    },
  },
};
</script>

<style scoped>
.main {
  text-align: center; /*让div内部文字居中*/
  background-color: #fff;
  border-radius: 20px;
  width: 400px;
  height: 350px;
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  font-weight: bold;
  font-size: 40px;
}

.demo-image__lazy {
  height: 600px;
  overflow-y: auto;
}
.demo-image__lazy .el-image {
  display: block;
  min-height: 200px;
  margin-bottom: 10px;
}
.demo-image__lazy .el-image:last-child {
  margin-bottom: 0;
}
.el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: calc(100% - 30px);
  max-width: calc(100% - 30px);
}
.content {
  text-indent: 2em;
}
</style>